package leetcode.sort;

/**
 * @title: BubleSort
 * @Author jianglei
 * @Date: 2021/12/12 17:25
 * @Version 1.0
 */
public class BubleSort {

    public static void main(String[] args) {
        int [] arr = new int[]{1,3,2,5,8,5,7,4,2,10,19,13,12};
        new BubleSort().bubleSort(arr);
        for (int i : arr) {
            System.out.print(i+" ");
        }
    }

    private void bubleSort(int []nums){
        boolean flag;
        for (int i = 1; i < nums.length; i++) {
            flag =false;
            for (int j = 1; j < nums.length-i+1 ; j++) {
                if(nums[j]<nums[j-1]){
                    swap(nums,j,j-1);
                    flag =true;
                }
            }
            if(!flag)
                break;
        }
    }

    private void swap(int []nums,int i,int j){
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}